package com.wyp168.servicecms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyp168.commonutils.R;
import com.wyp168.servicecms.entity.CrmBanner;
import com.wyp168.servicecms.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author wyp168
 * @since 2022-05-17
 */
@RestController
@RequestMapping("/servicecms/banneradmin")
@CrossOrigin
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;

    //分页查询Banner
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page , @PathVariable long limit) {
        Page<CrmBanner> bannerPage = new Page<>(page,limit);
        bannerService.page(bannerPage);

        return R.ok().data("items",bannerPage.getRecords()).data("total",bannerPage.getTotal());
    }


    //添加Banner
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        bannerService.save(crmBanner);
        return R.ok();
    }

    //删除Banner
    @DeleteMapping("deleteBanner/{id}")
    public R deleteBanner(@PathVariable long id) {
        bannerService.removeById(id);
        return R.ok();
    }

    //根据Id获取Banner信息
    @GetMapping("getBannerById/{id}}")
    public R getById(@PathVariable long id) {
        CrmBanner bannerInfo = bannerService.getById(id);
        return R.ok().data("bannerInfo",bannerInfo);
    }

    //更新Banner
    @PutMapping
    public R updateById(@RequestBody CrmBanner crmBanner) {
        bannerService.updateById(crmBanner);
        return R.ok();
    }
}

